using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using SharpMud.MudLib.Common.Entities.Players.Persistance;
using SharpMud.MudLib.Common.Entities.Players.Session;
using SharpMud.MudLib.Common.Entities.Players.Session.States;
using SharpMud.Net;

namespace SharpMud.MudLib.Common.Entities.Players
{
    [XmlRoot("PlayerManagerConfiguration")]
    public class PlayerManagerConfiguration<TPlayerCharacterInstance> where TPlayerCharacterInstance : IPlayerCharacter, new()
    {
        private List<PlayerCharacterLoaderMetadatum<TPlayerCharacterInstance>> _PlayerLoaders;
        private bool    _LogAllPfileEvents;
        private string  _StartingTelnetSessionState;

        public PlayerManagerConfiguration()
        {
            _PlayerLoaders = new List<PlayerCharacterLoaderMetadatum<TPlayerCharacterInstance>>();
            _LogAllPfileEvents = true;
        }

        [XmlAttribute("LogAllPfileEvents")]
        public bool LogAllPfileEvents
        {
            get
            {
                return _LogAllPfileEvents;
            }
            set
            {
                _LogAllPfileEvents = value;
            }
        }

        [XmlElement("StartingTelnetSessionState")]
        public string StartingTelnetSessionState
        {
            get
            {
                return _StartingTelnetSessionState;
            }
            set
            {
                _StartingTelnetSessionState = value;
            }
        }

        [XmlIgnore()]
        public Type StartingTelnetSessionStateType
        {
            get
            {
                return System.Type.GetType(_StartingTelnetSessionState, false, true);
            }
            set
            {
                _StartingTelnetSessionState = value.AssemblyQualifiedName;
            }
        }

        public ISessionState<TelnetPlayerSession> GetStartingTelnetSessionState(INetworkConnection connection)
        {
            if (StartingTelnetSessionStateType == null)
                return null;
            else
                return (ISessionState<TelnetPlayerSession>)StartingTelnetSessionStateType.Assembly.CreateInstance(StartingTelnetSessionState,true);
        }

        public PlayerCharacterLoaderMetadatum<TPlayerCharacterInstance>[] PlayerLoaders
        {
            get
            {
                return _PlayerLoaders.ToArray();
            }
            set
            {
                _PlayerLoaders.AddRange(value);
            }
        }
    }
}
